package com.ujiuye.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;

/**
 * 监听servletcontext作用域值的变化
 */
//@WebListener
public class Demo01Listener implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        //添加值
        System.out.println("向servletcontext添加键值对");

        //获取添加键值对的key和value
        String name = scae.getName();
        Object value = scae.getValue();
        System.out.println(name + "...." + value);
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        //移除值
        System.out.println("向servletcontext移除键值对");

        //获取删除的键值对 key和value
        String name = scae.getName();
        Object value = scae.getValue();
        System.out.println(name + "...." + value);
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        //替换值
        System.out.println("向servletcontext替换键值对");

        //获取替换之前的key和value
        String name = scae.getName();
        Object value = scae.getValue();
        System.out.println(name + "...." + value);
    }
}
